from exts import db
from datetime import datetime


# 新表
class EmployeeModel(db.Model):
    __tablename__ = 'employees'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(255), nullable=False)
    email = db.Column(db.String(255), nullable=False, unique=True)
    role = db.Column(db.Integer, db.ForeignKey("role.identity"), nullable=False)


class DayPaperModel(db.Model):
    __tablename__ = 'daypaper'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    date = db.Column(db.Date, nullable=False)
    em_id = db.Column(db.Integer, db.ForeignKey("employees.id"), nullable=False)
    submitted = db.Column(db.Integer, db.ForeignKey("submission.status"), nullable=False, default=-1)
    weekday = db.Column(db.Integer)

    employee = db.relationship("EmployeeModel", backref="daypapers")  # 建立双重映射
    condition = db.relationship("SubmissionModel", backref="daypapers")  # 建立双重映射


class LoginModel(db.Model):
    __tablename__ = 'login'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(255), nullable=False)
    username = db.Column(db.String(255), nullable=False, unique=True)
    password = db.Column(db.String(255), nullable=False)
    # em_id = db.Column(db.Integer, db.ForeignKey("employees.id"), nullable=False)

    # employee = db.relationship("EmployeeModel", backref="account")  # 建立双重映射


class RoleModel(db.Model):
    __tablename__ = 'role'
    identity = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(255), nullable=False)

    employees = db.relationship("EmployeeModel", backref="identity")


class SubmissionModel(db.Model):
    __tablename__ = 'submission'
    status = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(255), nullable=False)


class ConfigurationModel(db.Model):
    __tablename__ = 'configuration'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(255), nullable=False)
    secretkey = db.Column(db.String(255), nullable=False)


class Leave_vacationModel(db.Model):
    __tablename__ = 'leave_vacation'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    em_id = db.Column(db.Integer, db.ForeignKey("employees.id"), nullable=False)
    status = db.Column(db.Integer, db.ForeignKey("submission.status"), nullable=False)
    reason = db.Column(db.String(255), nullable=False)
    start_date = db.Column(db.Date, nullable=False)
    end_date = db.Column(db.Date, nullable=False)

    employee = db.relationship("EmployeeModel", backref="leave_vacations")  # 建立双重映射
    condition = db.relationship("SubmissionModel", backref="leave_vacations")  # 建立双重映射
